/* important this week widget */
$(document).ready(function(){
	Important.init();
});

var Important = {
	saveImportant:function(){
		if(!Important.$button.hasClass('fa-spin')){
			var name = Important.$name.val() || "";
			var sort_order = Important.$this.find('ul.checklist li').length + 1;
			if(name == ""){
				Important.$name.addClass('validation-failed');
			} else {
				Important.$name.removeClass('validation-failed');
				Important.$button.addClass('fa-spin');
				Important.$name.val('');
				Ajax.run({
					method:"importantService.saveImportant",
					onsuccess:function(data){
						Important.refresh();
					},
					args:{name:name,sort_order:sort_order}
				});
			}
		}
	},
	saveComplete:function(that){
		var $li = $(that).closest('li');
		$li.find('i').animate({opacity:0},300);
		Ajax.run({
			method:"importantService.saveComplete",
			onsuccess:function(data){
				Important.refresh();
			},
			args:{
				complete:$li.attr('complete'),
				id:$li.attr('importantID')
			}
		});
	},
	remove:function(that){
		var $this = $(that);
		if($this.hasClass('delete')){
			var $li = $this.closest('li');
			$li.fadeOut(150);
			Ajax.run({
				method:"importantService.remove",
				onsuccess:function(data){
					Important.refresh();
				},
				args:{id:$li.attr('importantID')}
			});
		} else {
			$this.addClass('delete');
			setTimeout(function(){
				$this.removeClass('delete');
			},2000);
		}
	},
	init:function(){
		Important.$this = $('#module-left-important-div');
		Important.$name = Important.$this.find('input[name=new_important]');
		Important.$button = Important.$this.find('.fa.important-add');
		Important.$contents = Important.$this.find('.important-contents');
		Important.$name.on('keyup',function(e){
			if(e.keyCode == 13){
				Important.saveImportant();
			}
		});
		
		$(document).on("click",'.check-important',function(){
			Important.saveComplete(this);
		});
		
		// hide the add bar after 6 items
		Important.assessNumberOfItems();
		
		//initiate sortable
		Important.initSort();
	},
	initSort:function(){
		var $checklist = $("ul.checklist");
		if($checklist.length > 0){
			$( "ul.checklist" ).not('.old').sortable({
				revert: true,
				update:function(){
					Important.saveSortOrder();
				},
				items:"> li.open"
		    });
		}
	},
	refresh:function(){
		Important.$button.addClass('fa-spin');
		Ajax.run({
			method:"importantView.renderImportant",
			onsuccess:function(data){
				Important.$contents.html(data);
				Important.$button.removeClass('fa-spin');
				// hide the add bar after 6
				Important.assessNumberOfItems();
				Important.initSort();
				Important.init();
				Important.$name.focus();
			}
		});
	},
	assessNumberOfItems:function(){
		if(Important.$contents.find('ul.checklist li').length >= 6){
			Important.$name.fadeOut(100);
			Important.$button.fadeOut(100);
			$('#module-left-important-div').find('a#important-add-more').removeClass('hidden');
			$('#module-left-important-div').find('#important-add-more2').addClass('hidden');
			$('[name=new_important]').hide();
		} else {
			Important.$name.fadeIn(100);
			Important.$button.fadeIn(100);
			$('#module-left-important-div').find('a#important-add-more').addClass('hidden');
			$('#module-left-important-div').find('#important-add-more2').addClass('hidden');
			$('[name=new_important]').show();
		}
	},
	showAddMore:function(){
		$('#module-left-important-div').find('a#important-add-more').addClass('hidden');
		$('#module-left-important-div').find('#important-add-more2').removeClass('hidden');
		Important.$name.fadeIn(100,function(){
			Important.$name.focus();	
		});
		Important.$button.fadeIn(100);
		$('[name=new_important]').show();
	},
	saveSortOrder:function(){
		var ids = "";
		$lis = $('ul.checklist').not('.done').find('li').each(function(){
			var $this = $(this);
			var id = $this.attr('importantID') || "";
			if(id.length > 0){
				if(ids != ""){
					ids += ",";
				}
				ids += $(this).attr('importantid');
			}
		});
		Ajax.run({
			method:"importantService.saveSortOrder",
			onsuccess:function(data){
				//console.log(ids);
			},
			args:{ids:ids}
		});
	},
	removeOld:function(that){
		var $this = $(that);
		var $li = $this.closest('li');
		$li.fadeOut(function(){
			$li.remove();
			// check how many are left to remove entire section
			var $ulOld = $('.checklist.old');
			var remaining = $ulOld.find('li').length;
			if(remaining == 0){
				$('#last-weeks-items').fadeOut();
			}
		});
		Ajax.run({
			method:"importantService.remove",
			args:{id:$li.attr('importantID')}
		});
	},
	addOld:function(that){
		var $this = $(that);
		var $li = $this.closest('li');
		var text = $li.find('.important-name').text();
		Important.$name.val(text);
		Important.saveImportant();
		Important.removeOld(that);
	}
};